﻿using MySql.Data.MySqlClient;
using StoreProject.OtherEntity.DB;
using StoreProject.OtherUtil;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StoreProject.DBUtil
{
    public class ValidDbSettingRule
    {
        /// <summary>
        /// 验证数据库设置
        /// </summary>
        /// <param name="opdSetting">数据库设置参数</param>
        /// <returns>连接成功返回true，否则返回false</returns>
        public static bool ValidDBSetting(DBConnSetting opdSetting)
        {
            bool success = true;
            MySqlConnection conn = null;
            try
            {
                MySqlHelper helper = new MySqlHelper(opdSetting.Server, opdSetting.DbName, opdSetting.User, opdSetting.Psw);
                conn = helper.getConnection();
                if (conn == null)
                {
                    success = false;
                }
                else
                {
                    
                    Task.Run(() =>
                    {
                        BackUpDb(opdSetting);
                    });
                }
            }
            catch
            {
                success = false;
            }
            finally
            {
                if (conn != null)
                    conn.Close();
            }
            return success;
        }
        private static void BackUpDb(DBConnSetting opdSetting)
        {
            try
            {
                IniConfig iniConfig = new IniConfig("DbSetting.ini");
                string path = iniConfig.Read("BackPath", "MySQLSetting");
                string FileName = path + "lingtong" + DateTime.Now.Date.ToString("yyyyMMdd") + ".sql";
                BackDb(FileName, opdSetting);
            }
            catch { }
        }
        private static void BackDb(string fileName, DBConnSetting opdSetting)
        {
            try
            {
                Process pro = new Process();
                pro.StartInfo.RedirectStandardInput = true;
                pro.StartInfo.RedirectStandardOutput = true;
                pro.StartInfo.RedirectStandardError = true;
                pro.StartInfo.CreateNoWindow = true;
                pro.StartInfo.FileName = "cmd.exe";
                pro.EnableRaisingEvents = true;
                pro.StartInfo.UseShellExecute = false;
                //pro.ErrorDataReceived += new DataReceivedEventHandler(pro_ErrorDataReceived);
                //pro.Exited += new EventHandler(pro_Exited);
                string ss = "mysqldump -h " + opdSetting.Server + " -u" + opdSetting.User + " -p" + opdSetting.Psw + " " + opdSetting.DbName + " > " + fileName;
                pro.Start();
                pro.StandardInput.WriteLine(ss);
                pro.BeginErrorReadLine();
               string error = pro.StandardOutput.ReadLine();
                pro.StandardInput.WriteLine("exit");
                pro.WaitForExit();
                pro.Close();
            }
            catch { }
        }
    }
}
